package com.xiaoshuidi.cloud.module.report.xxljob;

import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.module.report.constant.ContractStatusEnum;
import com.xiaoshuidi.cloud.module.report.constant.CustomTypeEnum;
import com.xiaoshuidi.cloud.module.report.dal.DO.RoomHouseDO;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.ReportRentPeriodDetailsDO;
import com.xiaoshuidi.cloud.module.report.dal.mapper.ContractRentorderMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.RoomHouseMapper;
import com.xiaoshuidi.cloud.module.report.dal.mapper.report.ReportRentPeriodDetailsMapper;
import com.xiaoshuidi.cloud.module.report.xxljob.PO.ContractPeriodPO;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 处理租金周期明细表job每日8点
 */
@Component
@Slf4j
public class ReportRentPeriodDetailsHandleJob {

    private static final String PAYED="已支付";

    @Resource
    private ReportRentPeriodDetailsMapper reportRentPeriodDetailsMapper;
    @Resource
    private ContractRentorderMapper contractRentorderMapper;
    @Resource
    private RoomHouseMapper roomHouseMapper;

    @XxlJob("reportRentPeriodDetailsHandleJob")
    @TenantIgnore
    public void execute() {
        //获取所有租金周期明细表数据 分2000批次处理
        //分2000一批次处理
        int pageNo = 0;
        int pageSize = 2000;
        List<ReportRentPeriodDetailsDO> list;
        do {
            int from = pageNo * pageSize;
            list = reportRentPeriodDetailsMapper.selectAllDetailsByPage(from, pageSize);
            handleLedgerList(list);
            pageNo++;
        } while (list.size() == 2000);
    }

    /**
     * 填充相关数据到台账表，方便查询
     * @param list
     */
    private void handleLedgerList(List<ReportRentPeriodDetailsDO> list){
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //房源 账单支付日期
        for (ReportRentPeriodDetailsDO infoDO : list) {
            if(ContractStatusEnum.NO_SIGNED.getStatus().equals(infoDO.getContractStatus())||ContractStatusEnum.PENDING.getStatus().equals(infoDO.getContractStatus())){
                //未签约的不处理
                continue;
            }
            try {
                if(infoDO.getHouseResource() == null){
                    //拼凑房源名称  只有首次为空时需要拼接 后续跑批不再拼接
                    RoomHouseDO houseDO = roomHouseMapper.selectById(infoDO.getRoomId());
                    if(houseDO ==null){
                        log.error("=====脏数据，有房间id:{},但是没有房源信息！ ",infoDO.getRoomId());
                        continue;
                    }
                    infoDO.setCustomerType(CustomTypeEnum.getValueByType(infoDO.getCustomerType()));
                    //集中式和分散式拼凑方法不一样
                    StringBuilder houseName = new StringBuilder(houseDO.getBuildingNo()).append("栋");
                    if(houseDO.getIsConcentrated()){
                        //集中式 小区名-楼栋-房源名称   room_house.area - room_house.building_no栋 - room_house.name
                        infoDO.setHouseResource(houseName.append(houseDO.getName()).toString());
                    }else {
                        //分散式  小区名-楼栋-单元-房源名称 room_house.area - room_house.building_no栋 - room_house.unit_no单元 - room_house.name
                        infoDO.setHouseResource(houseName.append(houseDO.getUnitNo()).append("单元").append(houseDO.getName()).toString());
                    }
                    infoDO.setArea(houseDO.getArea());
                }

                infoDO.setHandleFlag(1);
                //获取账单周期
                List<ContractPeriodPO> periodList = contractRentorderMapper.selectContractPeriodByContractIds(infoDO.getContractId());
                for (ContractPeriodPO e : periodList) {
                    switch (e.getNum()){
                        case 1:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setOne(PAYED);
                            }else {
                                infoDO.setOne(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 2:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setTwo(PAYED);
                            }else {
                                infoDO.setTwo(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 3:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setThree(PAYED);
                            }else {
                                infoDO.setThree(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 4:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setFour(PAYED);
                            }else {
                                infoDO.setFour(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 5:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setFive(PAYED);
                            }else {
                                infoDO.setFive(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 6:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setSix(PAYED);
                            }else {
                                infoDO.setSix(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 7:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setSeven(PAYED);
                            }else {
                                infoDO.setSeven(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 8:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setEight(PAYED);
                            }else {
                                infoDO.setEight(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 9:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setNine(PAYED);
                            }else {
                                infoDO.setNine(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 10:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setTen(PAYED);
                            }else {
                                infoDO.setTen(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 11:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setEleven(PAYED);
                            }else {
                                infoDO.setEleven(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        case 12:
                            if(e.getPayStatus().equals("payment")){
                                infoDO.setTwelve(PAYED);
                            }else {
                                infoDO.setTwelve(sdf.format(e.getOughtPayTime()));
                            }
                            break;
                        default:
                    }
                }
            } catch (Exception e) {
                log.error("=====收款信息处理失败====roomId:{}",infoDO.getRoomId());
                e.printStackTrace();
            }

        }
        reportRentPeriodDetailsMapper.updateBatch(list,list.size());
    }


}
